package com.zzy.hero.controller;


import com.zzy.hero.entity.Hero;
import com.zzy.hero.service.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author 邹靓仔
 * @date 2020/6/10 -9:45 -zzy-crm
 **/
@Controller
@RequestMapping("/hero")
public class HeroController {

    @Autowired
    private HeroService heroService;


    @GetMapping("/selectList")
    public String selectList(Model model){
        List<Hero> heroes = heroService.selectList();
        model.addAttribute("heroes", heroes);
        return "hero";
    }


    @RequestMapping(value = "/delete", method = RequestMethod.GET)

    public String delete(int id){
        heroService.delete(id);
        return "redirect:/hero/selectList";
    }


    @RequestMapping(value = "/toEdit" , method = RequestMethod.GET)
    public String toEdit(Model model, int id) {
        Hero hero = heroService.findHeroById(id);
        model.addAttribute("hero", hero);
        return "hero-edit";
    }

    @RequestMapping(value = "/update" , method = RequestMethod.POST)
    public String updateCustomer(Hero hero){
        heroService.update(hero);
        return "redirect:/hero/selectList";
    }

    @RequestMapping(value = "/toAdd" , method = RequestMethod.GET)
    public String addCus() {
        return "hero-add";
    }

    @PostMapping("/add")
    public String add(Hero hero){
        heroService.add(hero);
        return "redirect:/hero/selectList";
    }


}
